package com.yanfan.framework.aspectj;

import com.yanfan.common.annotation.CacheBatchEvict;
import com.yanfan.framework.utils.CacheBatchAnnotationUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * 示例：
 * @CacheBatchEvict(cacheNames = "device", key = "'xxxxx_*_' + #product.productId")
 *
 * @description:
 * @author: lsy
 * @date: 2024年12月22日 23:55:41
 */
@Slf4j
@Aspect
@Component
public class CacheBatchEvictAspect {

    /**
     * 定义切入点
     */
    @Pointcut("@annotation(com.yanfan.common.annotation.CacheBatchEvict)")
    public void pointcut() {

    }

    @Around("pointcut()")
    public Object proceed(ProceedingJoinPoint joinPoint) throws Throwable {
        // 1.获取注解参数值 SpEL表达式
        Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
        Object[] args = joinPoint.getArgs();
        CacheBatchEvict cacheBatchEvict =  method.getAnnotation(CacheBatchEvict.class);
        CacheBatchAnnotationUtils.cacheBatchEvict(method, args, cacheBatchEvict);
        // 4.执行目标方法
        return joinPoint.proceed();
    }

}
